Option Explicit On
Public velocidad As Double
Dim x, y As Double
Dim incx, incy As Integer
Dim mover As Boolean
Dim hini, hfin As Date
Public gRed As Integer
Public gGreen As Integer
Public gBlue As Integer
Public tipo_color As Integer

Private Sub UserForm_Click()
    If mover = True Then
        mover = False
    Else
        frmControl.Show()
        DoEvents()
        btnPel.BackColor = RGB(gRed, gGreen, gBlue)
        If x > Me.Height - btnPel.Height Then x = 1
        If y > Me.Width - btnPel.Width Then y = 1
        hini = Time
        hfin = DateAdd("n", 3, hini)
        mover = True
        mueve()
    End If
End Sub

Private Sub UserForm_Initialize()
    MsgBox("Haz clic sobre la pantalla para iniciar el cuadro de dilogo y opciones de movimiento." & vbCrLf & "Haz clic sobre el botn que se mueve para finalizar o cierra la ventana.")
    x = 1
    y = 1
    incx = 1
    incy = 1
    tipo_color = 1 ' 1 - Automtico, 2 - Seleccionado por usuario
    velocidad = 500000
    gRed = 0
    gGreen = 0
    gBlue = 0
    btnPel.BackColor = RGB(gRed, gGreen, gBlue)
    Me.Height = 400
    Me.Width = 400
End Sub

Private Sub btnPel_Click()
    MsgBox("Adios!!")
    Unload(Me)
End Sub

Sub mueve()
    hini = Time
    hfin = DateAdd("n", 3, hini) ' Tiempo de seguridad
    Dim topex As Double
    Dim topey As Double
    topex = Me.Height - 40
    topey = Me.Width - 25
    Do While mover = True
        Dim i As Double
        For i = 1 To velocidad
        Next i
        If x > topex Or x < 1 Then
            incx = -incx
        End If
        x = x + incx
        If y > topey Or y < 1 Then
            incy = -incy
        End If
        y = y + incy
        btnPel.Move(Left:=y, Top:=x, Layout:=True)
        If tipo_color = 1 Then
            gBlue = gBlue + 10
            If gBlue > 255 Then
                gBlue = 0
                gGreen = gGreen + 10
                If gGreen > 255 Then
                    gGreen = 0
                    gRed = gRed + 10
                    If gRed > 255 Then gRed = 0
                End If
            End If
        End If
        btnPel.BackColor = RGB(gRed, gGreen, gBlue)
        frmAnimacion.Repaint()
        DoEvents()
        If Time > hfin Then mover = False
    Loop
End Sub

Private Sub UserForm_Terminate()
    mover = False
    DoEvents()
End Sub
